home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Subversive Component Hacks / DropKeyFrames / DropKeyFrames.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.6 KB  |  174 lines

  1. #include <ImageCodec.h>
  2.  
  3. #include "DropKeyFrames.h"
  4.  
  5. // data structures
  6.  
  7. typedef struct {
  8.     Component victim;
  9. } DropKeyFramesSharedGlobalsRecord, **DropKeyFramesSharedGlobalsHandle;
  10.  
  11. typedef struct{
  12.     ComponentInstance                    self;
  13.     ComponentInstance                    delegateComponent;
  14.     ComponentInstance                    target;
  15.     DropKeyFramesSharedGlobalsHandle    sharedGlobals;
  16.     UInt32                                beginCount;
  17.     UInt32                                drawCount;
  18.     UInt32                                endCount;
  19. } DropKeyFramesGlobalsRecord, *DropKeyFramesGlobalsPtr;
  20.  
  21. // dispatcher
  22.  
  23. #define IMAGECODEC_BASENAME()             DropKeyFrames
  24. #define IMAGECODEC_GLOBALS()             DropKeyFramesGlobalsPtr storage
  25.  
  26. #define CALLCOMPONENT_BASENAME()        IMAGECODEC_BASENAME()
  27. #define    CALLCOMPONENT_GLOBALS()            IMAGECODEC_GLOBALS()
  28.  
  29. #define COMPONENT_UPP_PREFIX()            uppImageCodec
  30. #define COMPONENT_DISPATCH_FILE            "DropKeyFramesDispatch.h"
  31. #define COMPONENT_SELECT_PREFIX()          kImageCodec
  32.  
  33. #define    GET_DELEGATE_COMPONENT()        (storage->delegateComponent)
  34.  
  35. #include "Components.k.h"
  36. #include "ImageCodec.k.h"
  37. #include "ComponentDispatchHelper.c"
  38.  
  39. // code
  40.  
  41. pascal ComponentResult DropKeyFramesOpen(DropKeyFramesGlobalsPtr glob, ComponentInstance self)
  42. {
  43.     ComponentResult err;
  44.     DropKeyFramesSharedGlobalsHandle sharedGlobals;
  45.  
  46.     glob = (DropKeyFramesGlobalsPtr)NewPtrClear(sizeof(DropKeyFramesGlobalsRecord));
  47.     err = MemError();
  48.     if (noErr != err) goto bail;
  49.  
  50.     SetComponentInstanceStorage(self, (Handle)glob);
  51.     glob->self = self;
  52.     glob->target = self;
  53.  
  54.     sharedGlobals = (DropKeyFramesSharedGlobalsHandle)GetComponentRefcon((Component)self);
  55.     if (!sharedGlobals) {
  56.         Component victim;
  57.         ComponentDescription cd;
  58.  
  59.         GetComponentInfo((Component)self, &cd, nil, nil, nil);
  60.         cd.componentManufacturer = kVictimManufacturer;
  61.         cd.componentFlags = 0;
  62.         cd.componentFlagsMask = 0;
  63.         victim = FindNextComponent(nil, &cd);
  64.         if (!victim) {
  65.             DebugStr("\p couldn't find victim!");
  66.             err = paramErr;
  67.             goto bail;
  68.         }
  69.  
  70.         sharedGlobals = (DropKeyFramesSharedGlobalsHandle) NewHandleSysClear(sizeof(DropKeyFramesSharedGlobalsRecord));
  71.         if (!sharedGlobals) {
  72.             err = MemError();
  73.             goto bail;
  74.         }
  75.         SetComponentRefcon((Component)self, (long)sharedGlobals);
  76.  
  77.         victim = CaptureComponent(victim, (Component)self);
  78.         if (!victim) {
  79.             err = paramErr;
  80.             goto bail;
  81.         }
  82.         (**sharedGlobals).victim = victim;
  83.     }
  84.  
  85.     glob->sharedGlobals = sharedGlobals;
  86.     err = OpenAComponent((**sharedGlobals).victim, &glob->delegateComponent);
  87.     if (err) goto bail;
  88.  
  89.     ComponentSetTarget(glob->delegateComponent, self);
  90.  
  91. bail:
  92.     return err;
  93. }
  94.  
  95. pascal ComponentResult DropKeyFramesRegister(DropKeyFramesGlobalsPtr glob)
  96. {
  97.     // If we get this far, we must have been opened successfully, so all is well.
  98.     return noErr;
  99. }
  100.  
  101. pascal ComponentResult DropKeyFramesUnregister(DropKeyFramesGlobalsPtr glob)
  102. {
  103.     UncaptureComponent((**glob->sharedGlobals).victim);
  104.     DisposeHandle((Handle)glob->sharedGlobals);
  105.     return noErr;
  106. }
  107.  
  108. pascal ComponentResult DropKeyFramesClose(DropKeyFramesGlobalsPtr glob, ComponentInstance self)
  109. {
  110.     if (glob) {
  111.         if (glob->delegateComponent)
  112.             CloseComponent(glob->delegateComponent);
  113.  
  114.         DisposePtr((Ptr)glob);
  115.     }
  116.  
  117.     return noErr;
  118. }
  119.  
  120. pascal ComponentResult DropKeyFramesTarget(DropKeyFramesGlobalsPtr glob, ComponentInstance target)
  121. {
  122.     glob->target = target;
  123.     if (glob->delegateComponent)
  124.         ComponentSetTarget(glob->delegateComponent, target);
  125.  
  126.     return noErr;
  127. }
  128.  
  129. pascal ComponentResult DropKeyFramesVersion(DropKeyFramesGlobalsPtr glob)
  130. {
  131.     return CallComponentVersion(glob->delegateComponent);
  132. }
  133.  
  134. static Boolean capsLockIsDown(void)
  135. {
  136.     KeyMapByteArray    keys;
  137.     GetKeys((UInt32*)keys);
  138.     return (keys[7] & 2) ? true : false;
  139. }
  140.  
  141. // Based on empirical observations, this appears to be true for Sorenson frames.
  142. #define isKeyFrame(drp)    (((*(UInt32*)drp->codecData) & 1) == 0)
  143.  
  144. pascal ComponentResult DropKeyFramesBeginBand(DropKeyFramesGlobalsPtr glob, CodecDecompressParams *p, ImageSubCodecDecompressRecord *drp, long flags)
  145. {
  146.     Boolean okayToDrop = (0 != glob->beginCount++); // don't drop first frame
  147.     
  148.     if(okayToDrop && isKeyFrame(drp))
  149.         return noErr;
  150.     else
  151.         return ImageCodecBeginBand(glob->delegateComponent, p, drp, flags);
  152. }
  153.  
  154. pascal ComponentResult DropKeyFramesDrawBand(DropKeyFramesGlobalsPtr glob, ImageSubCodecDecompressRecord *drp)
  155. {
  156.     Boolean okayToDrop = (0 != glob->drawCount++);
  157.     
  158.     if(okayToDrop && isKeyFrame(drp))
  159.         return noErr;
  160.     else
  161.         return ImageCodecDrawBand(glob->delegateComponent, drp);
  162. }
  163.  
  164. pascal ComponentResult DropKeyFramesEndBand(DropKeyFramesGlobalsPtr glob, ImageSubCodecDecompressRecord *drp, OSErr result, long flags)
  165. {
  166.     Boolean okayToDrop = (0 != glob->endCount++);
  167.     
  168.     if(okayToDrop && isKeyFrame(drp))
  169.         return noErr;
  170.     else
  171.         return ImageCodecEndBand(glob->delegateComponent, drp, result, flags);
  172. }
  173.  
  174.